package com.base;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import java.util.Stack;


/**
 * @author zhouzechao
 * Activity栈控制类
 */
class ActivityHelper implements Application.ActivityLifecycleCallbacks {
    private static int mActivityCount = 0;

    private static ActivityHelper instance;
    private Stack<Activity> mStack;
    private OnAppListener mAppExistListener;

    private ActivityHelper() {
        mAppExistListener = null;
        mStack = new Stack<>();
    }

    public static ActivityHelper getInstance() {
        if (instance == null) {
            synchronized (ActivityHelper.class) {
                if (instance == null) {
                    instance = new ActivityHelper();
                }
            }
        }
        return instance;
    }

    public boolean isBackground() {
        return mActivityCount < 1;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        mStack.add(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {

        if (mActivityCount == 0) {
            if (mAppExistListener != null) {
                mAppExistListener.onAppForground(activity);
            }
        }

        mActivityCount++;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        mActivityCount--;

        if (mActivityCount == 0) {
            if (mAppExistListener != null) {
                mAppExistListener.onAppBackground();
            }
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        mStack.remove(activity);
        if (mStack.isEmpty()) {
            if (mAppExistListener != null) {
                mAppExistListener.onAppExist();
            }
        }
    }

    public Activity getCurrentActivity() {
        return mStack.lastElement();
    }

    public Activity getCurrentActivity(String classname) {
        for (int i = 0; i < mStack.size(); i++) {
            Activity activity = mStack.get(i);
            if (activity.getClass().getName().equals(classname)) {
                return activity;
            }
        }
        return null;
    }

    public Activity getH5ReBackActivity() {
        if (!mStack.isEmpty() && mStack.size() > 1) {
            return mStack.get(mStack.size() - 2);
        }
        return null;
    }

    public int getActivitySize() {
        return mStack.size();
    }

    public void finishCurrentActivity() {
        Activity act = mStack.lastElement();
        if (act != null) {
            act.finish();
        }
    }

    public void finishActivity(Class<?> cls) {
        for (Activity act : mStack) {
            if (act.getClass().equals(cls)) {
                act.finish();
            }
        }
    }

    public void finishAllActivity() {
        for (int i = 0; i < mStack.size(); i++) {
            Activity act = mStack.get(i);
            if (act != null) {
                act.finish();
            }
        }
    }

    public void setOnAppListener(OnAppListener l) {
        mAppExistListener = l;
    }

    public interface OnAppListener {
        void onAppExist();

        void onAppBackground();

        void onAppForground(Activity activity);
    }
}
